﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BDD.TicTacToe.Domain;
using BDD.TicTacToe.Ui.Mvc.Models;

namespace BDD.TicTacToe.Ui.Mvc.Controllers
{
    public class GamePlayController : Controller
    {
        private readonly IGameRepository _gameRepository;

        public GamePlayController(IGameRepository gameRepository)
        {
            _gameRepository = gameRepository;            
        }

        //
        // GET: /GamePlay/Display/355-355-345345-345345-345

        public ActionResult Display(Guid gameId)
        {
            var game = _gameRepository.FindBy(gameId);

            var gameDisplayViewModel = new GameDisplayViewModel()
                                           {
                                               NextPlayerToPlaceToken = game.NextPlayerToPlaceToken
                                           };

            return View(gameDisplayViewModel);
        }

        //
        // POST: /GamePlay/PlaceToken/

        public ActionResult PlaceToken(PlaceTokenViewModel placeTokenViewModel)
        {
            // 1. Retrieve game from repository
            var game = _gameRepository.FindBy(placeTokenViewModel.GameId);

            // 2. Update Game
            var square = new Monkey();
            game.PlaceTokenIn(square);

            // 3. Save Game 
            _gameRepository.Save(game);

            // 4. Redirect to display action.
            return RedirectToAction("Display", new { gameId = placeTokenViewModel.GameId });
        }
    }    
}
